<style lang="less">
@import "./carTerminalManage.less";
</style>
<template>
  <div class="search">
    <Row>
      <Col>
        <Card>
          <Row>
            <Form
              ref="searchForm"
              :model="searchForm"
              inline
              :label-width="70"
              @submit.native.prevent
              class="search-form"
            >
              <Form-item label="设备名称">
                <Input
                  type="text"
                  v-model="searchForm.deviceName"
                  clearable
                  placeholder="请输入设备名称"
                  style="width: 200px"
                />
              </Form-item>
              <Form-item style="margin-left:-35px;" class="br">
                <Button @click="handleSearch" type="primary" icon="ios-search">搜索</Button>
                <Button @click="handleReset">重置</Button>
              </Form-item>
            </Form>
          </Row>
          <Row class="operation">
            <Button @click="add" type="primary" icon="md-add" v-has="'add'">添加设备</Button>
            <circleLoading v-if="operationLoading" />
          </Row>
          <!-- <Row>
            <Alert show-icon>
              已选择
              <span class="select-count">{{selectCount}}</span> 项
              <a class="select-clear" @click="clearSelectAll">清空</a>
            </Alert>
          </Row>-->
          <Row>
            <Table
              :loading="loading"
              max-height="600"
              border
              :columns="columns"
              :data="data"
              sortable="custom"
              @on-sort-change="changeSort"
              @on-selection-change="showSelect"
              ref="table"
            ></Table>
          </Row>
          <Row type="flex" justify="end" class="page">
            <Page
              :current="searchForm.current"
              :total="total"
              :page-size="searchForm.size"
              @on-change="changePage"
              @on-page-size-change="changePageSize"
              :page-size-opts="[10,20,50,100]"
              size="small"
              show-total
              show-elevator
              show-sizer
            ></Page>
          </Row>
        </Card>
      </Col>
    </Row>
    <Modal
      :title="modalTitle"
      v-model="carTerminalModalVisible"
      :mask-closable="false"
      :width="600"
      :styles="{top: '30px'}"
    >
      <Form
        ref="carTerminalForm"
        :model="carTerminalForm"
        :label-width="140"
        :rules="carTerminalFormValidate"
        style="padding-right:50px;"
      >
        <FormItem label="设备名称" prop="deviceName">
          <Input v-model="carTerminalForm.deviceName" autocomplete="off" />
        </FormItem>
        <FormItem label="设备编号" prop="deviceNum">
          <Input v-model="carTerminalForm.deviceNum" autocomplete="off" />
        </FormItem>
        <FormItem label="设备型号" prop="deviceModel">
          <Input v-model="carTerminalForm.deviceModel" />
        </FormItem>
        <FormItem label="生产厂家" prop="manufacturer">
          <Input v-model="carTerminalForm.manufacturer" />
        </FormItem>
        <FormItem label="卡号" prop="simCard">
          <Input v-model="carTerminalForm.simCard" placeholder="根据IMEI后10位自动生成" readonly />
        </FormItem>
        <FormItem label="IMEI" prop="imei">
          <Input v-model="carTerminalForm.imei" @on-blur="onImeiBlur" />
        </FormItem>
        <FormItem label="ICCID" prop="iccid">
          <Input v-model="carTerminalForm.iccid" />
        </FormItem>
        <FormItem label="出厂序列号" prop="sn">
          <Input v-model="carTerminalForm.sn" />
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" @click="carTerminalModalVisible = false">取消</Button>
        <Button type="primary" :loading="submitLoading" @click="submitCarTerminal">提交</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import {
  getCarTerminalPageList,
  addCarTerminal,
  editCarTerminal,
  deleteCarTerminal
} from "@/api/operate";
import circleLoading from "../../my-components/circle-loading.vue";
export default {
  name: "carTerminal-manage",
  components: {
    circleLoading
  },
  data() {
    return {
      loading: true,
      operationLoading: false,
      selectCount: 0,
      selectList: [],
      searchForm: {
        deviceName: "",
        current: 1,
        size: 20,
        sort: "createTime",
        order: "desc"
      },
      modalType: 0,
      carTerminalModalVisible: false,
      modalTitle: "",
      carTerminalForm: {
        deviceId: "",
        deviceName: "",
        deviceNum: "",
        deviceModel: "",
        manufacturer: "",
        simCard: "",
        imei: "",
        iccid: "",
        sn: ""
      },
      carTerminalFormValidate: {
        deviceName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        deviceNum: [
          { required: true, message: "设备编号不能为空", trigger: "blur" }
        ],
        // deviceModel: [
        //   { required: true, message: "型号不能为空", trigger: "blur" }
        // ],
        // manufacturer: [
        //   { required: true, message: "生产厂家不能为空", trigger: "blur" }
        // ],
        simCard: [{ required: true, message: "卡号不能为空", trigger: "blur" }],
        imei: [{ required: true, message: "IMEI不能为空", trigger: "blur" }]
        // iccid: [{ required: true, message: "ICCID不能为空", trigger: "blur" }],
        // sn: [{ required: true, message: "出场序列号不能为空", trigger: "blur" }]
      },
      submitLoading: false,
      columns: [
        // {
        //   type: 'selection',
        //   width: 60,
        //   align: 'center',
        //   fixed: 'left'
        // },
        {
          title: "序号",
          type: "index",
          width: 65,
          align: "center",
          fixed: "left"
        },
        {
          title: "设备名称",
          key: "deviceName",
          width: 180
        },
        {
          title: "设备编号",
          key: "deviceNum",
          width: 180
        },
        {
          title: "设备型号",
          key: "deviceModel",
          width: 150
        },

        {
          title: "生产厂商",
          key: "manufacturer",
          width: 150
        },
        {
          title: "卡号",
          key: "simCard",
          width: 150
        },
        {
          title: "IMEI",
          key: "imei",
          width: 160
        },
        {
          title: "ICCID",
          key: "iccid",
          width: 200
        },
        {
          title: "出场序列号",
          key: "sn",
          width: 150
        },
        {
          title: "车主姓名",
          key: "username",
          width: 120
        },
        {
          title: "车主联系方式",
          key: "mobile",
          width: 160
        },
        {
          title: "vip开始时间",
          key: "vipStart",
          width: 180
        },
        {
          title: "vip结束时间",
          key: "vipEnd",
          width: 180
        },
        {
          title: "设备状态",
          key: "status",
          width: 150,
          render: (h, params) => {
            let status = params.row.status;
            return h(
              "span",

              status == 0 ? (status == 1 ? "在线" : "离线") : "未知"
            );
          }
        },
        {
          title: "最后的位置",
          key: "lastLocation",
          width: 200
        },
        {
          title: "最后位置时间",
          key: "lastLocationTime",
          width: 180
        },

        {
          title: "创建时间",
          key: "createDate",
          width: 180
        },
        {
          title: "操作",
          key: "action",
          width: 200,
          align: "center",
          fixed: "right",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "warning",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  directives: [
                    {
                      name: "has",
                      value: "edit"
                    }
                  ],
                  on: {
                    click: () => {
                      this.edit(params.row);
                    }
                  }
                },
                "编辑"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "error",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  directives: [
                    {
                      name: "has",
                      value: "delete"
                    }
                  ],
                  on: {
                    click: () => {
                      this.remove(params.row);
                    }
                  }
                },
                "删除"
              )
            ]);
          }
        }
      ],
      data: [],
      total: 0
    };
  },
  methods: {
    init() {
      this.getCarTerminalList();
    },
    changePage(v) {
      this.searchForm.current = v;
      this.getCarTerminalList();
      this.clearSelectAll();
    },
    changePageSize(v) {
      this.searchForm.size = v;
      this.getCarTerminalList();
    },
    getCarTerminalList() {
      // 多条件搜索
      this.loading = true;
      getCarTerminalPageList(this.searchForm).then(res => {
        this.loading = false;
        if (res.code == 0) {
          this.data = res.data.records;
          this.total = res.data.total;
        }
      });
    },
    handleSearch() {
      this.searchForm.current = 1;
      this.searchForm.size = 20;
      this.getCarTerminalList();
    },
    handleReset() {
      this.$refs.searchForm.resetFields();
      this.searchForm.current = 1;
      this.searchForm.size = 20;
      // 重新加载数据
      this.getCarTerminalList();
    },
    changeSort(e) {
      this.searchForm.sort = e.key;
      this.searchForm.order = e.order;
      if (e.order == "normal") {
        this.searchForm.order = "";
      }
      this.getCarTerminalList();
    },
    onImeiBlur() {
      if (this.carTerminalForm.imei.length > 10) {
        this.carTerminalForm.simCard = this.carTerminalForm.imei.substring(
          this.carTerminalForm.imei.length - 10
        );
      }
    },

    submitCarTerminal() {
      this.$refs.carTerminalForm.validate(valid => {
        if (valid) {
          if (this.modalType == 0) {
            this.submitLoading = true;
            addCarTerminal(this.carTerminalForm).then(res => {
              this.submitLoading = false;
              if (res.code == 0) {
                this.$Message.success("操作成功");
                this.getCarTerminalList();
                this.carTerminalModalVisible = false;
              } else {
                this.$Message.error(res.msg);
              }
            });
          } else {
            // 编辑
            this.submitLoading = true;

            editCarTerminal(this.carTerminalForm).then(res => {
              this.submitLoading = false;
              if (res.code == 0) {
                this.$Message.success("操作成功");
                this.getCarTerminalList();
                this.carTerminalModalVisible = false;
              } else {
                this.$Message.error(res.msg);
              }
            });
          }
        }
      });
    },
    add() {
      this.modalType = 0;
      this.modalTitle = "新增设备";
      this.$refs.carTerminalForm.resetFields();
      this.carTerminalModalVisible = true;
    },
    edit(v) {
      this.modalType = 1;
      this.modalTitle = "编辑设备";
      this.$refs.carTerminalForm.resetFields();
      for (let attr in v) {
        if (v[attr] == null) {
          v[attr] = "";
        }
      }
      this.carTerminalForm = JSON.parse(JSON.stringify(v));
      this.carTerminalModalVisible = true;
    },
    remove(v) {
      this.$Modal.confirm({
        title: "确认删除",
        content: "您确认要删除设备 " + v.deviceName + " ?",
        loading: true,
        onOk: () => {
          deleteCarTerminal(v.deviceTypeId).then(res => {
            this.$Modal.remove();
            if (res.code == 0) {
              this.$Message.success("删除成功");
              this.getCarTerminalList();
            } else {
              this.$Message.error(res.msg);
            }
          });
        }
      });
    },
    showSelect(e) {
      this.exportData = e;
      this.selectList = e;
      this.selectCount = e.length;
    },
    clearSelectAll() {
      this.$refs.table.selectAll(false);
    },
    delAll() {
      if (this.selectCount <= 0) {
        this.$Message.warning("您还未选择要删除的数据");
        return;
      }
      this.$Modal.confirm({
        title: "确认删除",
        content: "您确认要删除所选的 " + this.selectCount + " 条数据?",
        loading: true,
        onOk: () => {
          let ids = "";
          this.selectList.forEach(function(e) {
            ids += e.id + ",";
          });
          ids = ids.substring(0, ids.length - 1);
          deleteCarTerminal(ids).then(res => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success("删除成功");
              this.clearSelectAll();
              this.getUserList();
            }
          });
        }
      });
    }
  },
  mounted() {
    this.init();
  }
};
</script>
